//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ScientificWorkflow.ManagementStudioModels
{
    using System;
    using System.Windows.Media.Imaging;
    using Microsoft.Research.ScientificWorkflow.TridentUtilities;
    using Microsoft.Research.eResearch.Common.Linq;
    using SR = Microsoft.Research.DataLayer;

    /// <summary>
    /// DataReferenceProviderModel representation.
    /// </summary>
    public class DataReferenceProviderModel : BaseRegMgrModel
    {
        /// <summary>
        /// Is complete reference model?
        /// </summary>
        private bool completeReferenceProviderModel;

        /// <summary>
        /// Gets or sets a value indicating whether [complete reference provider model].
        /// </summary>
        /// <value>
        /// 	<c>true</c> if [complete reference provider model]; otherwise, <c>false</c>.
        /// </value>
        public bool CompleteReferenceProviderModel
        {
            get
            {
                return this.completeReferenceProviderModel;
            }
            set
            {
                this.completeReferenceProviderModel = value;
                this.RaisePropertyChanged("CompleteReferenceProviderModel");
            }
        }

        /// <summary>
        /// Gets or sets the assembly.
        /// </summary>
        /// <value>The assembly.</value>
        public string Assembly { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="DataReferenceProviderModel"/> class.
        /// </summary>
        /// <param name="name">The name.</param>
        public DataReferenceProviderModel(string name)
        {
            this.Name = name;
            this.Label = this.Name;
            this.Id = Guid.Empty;

            this.ItemIcon = CreateImage("pack://Application:,,,/Microsoft.Research.ScientificWorkflow.TridentUtilities;component/Images/ManagementStudio/DataSourcesSmall.png");

            this.CompleteReferenceProviderModel = false;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DataReferenceProviderModel"/> class.
        /// </summary>
        /// <param name="provider">The provider.</param>
        public DataReferenceProviderModel(SR.DataReferenceProvider provider)
        {
            this.Update(provider);

            provider.DataReferenceStores
                .ForEach(store =>
                    {
                        DataReferenceStoreModel model = new DataReferenceStoreModel(store, this);
                        model.Parent = this;
                        this.Children.Add(model);
                    });

            this.CompleteReferenceProviderModel = true;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DataReferenceProviderModel"/> class.
        /// </summary>
        /// <param name="provider">The provider.</param>
        public DataReferenceProviderModel(DataReferenceProviderModel provider)
        {
            this.Update(provider);

            provider.Children
                .ForEach(store =>
                {
                    DataReferenceStoreModel model = new DataReferenceStoreModel(store as DataReferenceStoreModel);
                    model.Parent = this;
                    this.Children.Add(model);
                });

            this.CompleteReferenceProviderModel = true;
        }

        /// <summary>
        /// Creates the clone.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns></returns>
        public static DataReferenceProviderModel CreateClone(DataReferenceProviderModel model)
        {
            DataReferenceProviderModel clonedModel = null;
            if (null != model)
            {
                clonedModel = new DataReferenceProviderModel(model);
            }

            clonedModel.CompleteReferenceProviderModel = model.CompleteReferenceProviderModel;
            return clonedModel;
        }

        /// <summary>
        /// Updates the model with the specified provider.
        /// </summary>
        /// <param name="provider">The provider.</param>
        public void Update(SR.DataReferenceProvider provider)
        {
            if (provider != null)
            {
                this.Id = ((SR.IObject)provider).ID;
                this.Name = provider.Name;
                this.Label = provider.Name;
                this.Description = provider.Description;

                this.Assembly = provider.ClassName;

                this.CreatedBy = provider.Created.User;
                this.CreatedDate = provider.Created.When;
                this.LastModifiedTime = provider.Updated.When;
                this.LastModifiedBy = provider.Updated.User;

                this.ItemIcon = CreateImage("pack://Application:,,,/Microsoft.Research.ScientificWorkflow.TridentUtilities;component/Images/ManagementStudio/DataSourcesSmall.png");
            }
        }

        /// <summary>
        /// Updates the specified model.
        /// </summary>
        /// <param name="model">The model.</param>
        public override void Update(BaseRegMgrModel model)
        {
            DataReferenceProviderModel provider = model as DataReferenceProviderModel;
            if (provider != null)
            {
                this.Id = provider.Id;
                this.Name = provider.Name;
                this.Label = provider.Name;
                this.Description = provider.Description;

                this.Assembly = provider.Assembly;

                this.CreatedBy = provider.CreatedBy;
                this.CreatedDate = provider.CreatedDate;
                this.LastModifiedTime = provider.LastModifiedTime;
                this.LastModifiedBy = provider.LastModifiedBy;

                this.ItemIcon = CreateImage("pack://Application:,,,/Microsoft.Research.ScientificWorkflow.TridentUtilities;component/Images/ManagementStudio/DataSourcesSmall.png");
            }
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        public override string ToString()
        {
            return this.Name;
        }

        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <exception cref="T:System.NullReferenceException">
        /// The <paramref name="obj"/> parameter is null.
        /// </exception>
        public override bool Equals(object obj)
        {
            bool equal = false;
            DataReferenceProviderModel equateParam = obj as DataReferenceProviderModel;
            if (equateParam != null &&
                equateParam.Id.Equals(this.Id) &&
                DataReferenceProviderModel.CheckEquality(equateParam.Name, this.Name) &&
                DataReferenceProviderModel.CheckEquality(equateParam.Description, this.Description) &&
                DataReferenceProviderModel.CheckEquality(equateParam.Assembly, this.Assembly))
            {
                equal = true;
            }

            return equal;
        }

        /// <summary>
        /// Serves as a hash function for a particular type.
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        /// <summary>
        /// Creates the image.
        /// </summary>
        /// <param name="imagePath">The image path.</param>
        /// <returns></returns>
        private BitmapImage CreateImage(string imagePath)
        {
            BitmapImage image = null;
            try
            {
                image = new BitmapImage(new Uri(imagePath, UriKind.Absolute));
            }
            catch (Exception ex)
            {
                TridentErrorHandler.HandleUIException(ex);
            }

            return image;
        }
    }
}
